package com.zhisheng.web17.demos.web;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 创建者： 智晟全哥
 *
 * 练习通过原生的请求对象和响应对象,对浏览器进行交互
 *
 */

@RestController
public class MyRequestAndResponseController {

    @GetMapping("/hello")
    public String hello(String name,int age){
        int i = 1/0;
        System.out.println("name="+name+",age="+age);
        return "这是服务器给浏览器响应的结果:"+name+","+age;
    }

    private String name;
    private int age;

    // 使用servlet原生的技术做请求和响应的处理!!!(了解)
    @GetMapping("/hello2")
    public void hello2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name2 = request.getParameter("name2");
        String age2 = request.getParameter("age2");
        name = name2;
        age = Integer.parseInt(age2);
        System.out.println("name="+name+",age="+age);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("name="+name+",age="+age);
    }

}
